package com.henanuniversity.dao.impl;

import com.henanuniversity.dao.OrdersDao;
import com.henanuniversity.po.Business;
import com.henanuniversity.po.Orders;
import com.henanuniversity.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrdersDaoImpl implements OrdersDao {
    Connection conn=null;
    PreparedStatement pst=null;
    ResultSet rs=null;
    @Override
    public int saveOrders(Orders orders) {
        int result=0;
        String sql="insert into orders values(null,?,?,?,?,?,0)";
        try {
            //获取连接
            conn= DBUtils.getConnection();
            pst=conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
            pst.setString(1,orders.getUserId());
            pst.setInt(2,orders.getBusinessId());
            pst.setString(3,orders.getOrderDate());
            pst.setDouble(4,orders.getOrderTotal());
            pst.setInt(5,orders.getDaId());
            pst.executeUpdate();
            //获取刚刚保存的订单的daId值
            rs=pst.getGeneratedKeys();
            if(rs.next()){
                result=rs.getInt(1);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {

            DBUtils.close(rs,pst);
        }
        return result;
    }

    @Override
    public Orders getOrdersById(Integer orderId) {
        Orders orders=null;
        StringBuffer sql = new StringBuffer();
        sql.append("select o.*, ");
        sql.append(" b.businessId bbusinessId,");
        sql.append(" b.businessName bbusinessName,");
        sql.append(" b.businessAddress bbusinessAddress,");
        sql.append(" b.deliveryPrice bdeliveryPrice");
        sql.append(" from orders o left join business b on o.businessId=b.businessId");
        sql.append(" where o.orderId=?");

        try {
            conn=DBUtils.getConnection();
            pst=conn.prepareStatement(sql.toString());
            pst.setInt(1,orderId);
            rs=pst.executeQuery();
            if(rs.next()){
                orders=new Orders();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));

                Business business = new Business();
                business.setBusinessId(rs.getInt("bbusinessId"));
                business.setBusinessName(rs.getString("bbusinessName"));
                business.setBusinessAddress(rs.getString("bbusinessAddress"));
                business.setDeliveryPrice(rs.getDouble("bdeliveryPrice"));
                orders.setBusiness(business);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.close(rs,pst);
        }
        return orders;
    }

    @Override
    public List<Orders> listOrderByUserId(String userId) {
        List<Orders> list=new ArrayList<>();
        StringBuffer sql=new StringBuffer();
        sql.append("select o.* ,");
        sql.append(" b.businessId bbusinessId, ");
        sql.append(" b.businessName bbusinessName,");
        sql.append(" b.deliveryPrice bdeliveryPrice ");
        sql.append(" from orders o left join business b on o.businessId=b.businessId");
        sql.append(" where o.userId=? ");

        try {
            conn=DBUtils.getConnection();
            pst=conn.prepareStatement(sql.toString());
            System.out.println("dao:"+userId);
            pst.setString(1,userId);
            rs=pst.executeQuery();
            while(rs.next()){
                Orders orders=new Orders();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));

                Business business=new Business();
                business.setBusinessId(rs.getInt("bbusinessId"));
                business.setBusinessName(rs.getString("bbusinessName"));
                business.setDeliveryPrice(rs.getDouble("bdeliveryPrice"));
                orders.setBusiness(business);
                list.add(orders);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.close(rs,pst);
        }
        return list;
    }
}
